<template>
  <div class="userList">
    <button @click="addlost">发布寻物</button>
    <div class="usertishi">-发布列表-</div>
    <div class="userlistMessage" v-for="(item, i) in userlist" :key="i">
      <div class="usertital">
        <span class="username">
          {{ item.lostName }}
        </span>
        <div class="svg">
          <svg class="usericon" aria-hidden="true" @click="changeList(item.no)">
            <use xlink:href="#icon-bianji"></use>
          </svg>
          <svg class="usericon" aria-hidden="true" @click="isDel(item.no)">
            <!-- delList(item.no) -->
            <use xlink:href="#icon-shanchu"></use>
          </svg>
        </div>
      </div>
      <div class="userdes">
        <span>
          {{ item.description }}
        </span>
      </div>
    </div>

    <div class="confirm" v-if="isDelet">
      <div class="frame">
        <span>是否删除</span>
        <div class="confirmbtn">
          <button @click="noDel">取消</button>
          <button @click="yesDel">确认</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLostList, deletList, getLostListByNo } from "@/api/api.js";

export default {
  data: function () {
    return {
      userlist: [],
      isDelet: false,
      thisno: "",
    };
  },
  methods: {
    addlost: function () {
      this.$router.push("/addlost");
    },
    updateList: async function () {
      let result = await getLostList(this.$store.state.user.userInfo.email);
      this.userlist = result.data.lostList;
    },
    //删除
    delList: async function (no) {
      deletList(no);
    },
    //改变
    changeList: async function (no) {
      // console.log(no);
      // let result = await getLostListByNo(no);
      // let fin = result.data.lostList[0].no;
      // console.log(fin);
      this.$router.push({ name: "changeList", params: { no: no } });
    },
    isDel: async function (no) {
      this.isDelet = true;
      this.thisno = no;
      // let high = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
      // // console.log(high);
      // let mtop = document.querySelector(".confirm").style;
      // mtop.top = `${high}`;
      let last = document.querySelector('.userlistMessage:nth-last-child(1)');
      if(this.isDelet){
        last.style.marginBottom = '1.8rem';
      }
    },
    noDel: function () {
      // console.log("不删除");
      this.isDelet = false;
    },
    yesDel: function () {
      // console.log("删除");
      this.isDelet = false;
      this.delList(this.thisno);
    },
  },
  mounted: async function () {
    let result = await getLostList(this.$store.state.user.userInfo.email);
    this.userlist = result.data.lostList;
  },
  watch: {
    userlist: function () {
      this.updateList();
    },
    thisno: function () {
      // console.log("变了");
    },
  },
  // props: ["content"],
};
</script>

<style lang="less">
.userList {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  button {
    width: 4rem;
    height: 1rem;
    border: none;
    border-radius: 0.5rem;
    outline: none;
    background-color: #6851ac;
    color: white;
    font-size: 0.4rem;
    letter-spacing: 0.1rem;
    padding-left: 0.1rem;
    margin-top: 0.5rem;
    box-shadow: 0rem 0rem 0.2rem 0.1rem rgba(0, 0, 0, 0.1);
  }
  .usertishi {
    margin-top: 0.5rem;
    letter-spacing: 0.1rem;
    padding-left: 0.1rem;
    color: #192a56;
    font-weight: 600;
  }
  .userlistMessage {
    letter-spacing: 0.03rem;
    text-align: justify;
    width: 6rem;
    // height: 2rem;
    border-radius: 0.4rem;
    box-shadow: 0rem 0rem 0.1rem 0rem rgba(0, 0, 0, 0.1);
    margin-top: 0.5rem;
    background-color: white;
    display: flex;
    flex-direction: column;
    align-items: center;
    &:nth-last-child(1) {
      margin-bottom: 1.8rem;
    }
    .usertital {
      width: 5.5rem;
      color: #192a56;
      padding: 0.25rem 0.2rem 0.2rem 0.2rem;
      border-bottom: solid 0.01rem #7f8fa6;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        height: 0.4rem;
        &:first-child {
          font-weight: 700;
        }
      }
      .username {
        font-size: 0.4rem;
        line-height: 0.4rem;
      }
      .svg {
        display: flex;
        align-items: center;
        .usericon {
          width: 0.5rem;
          height: 0.5rem;
          fill: #192a56;
          &:last-child {
            width: 0.42rem;
            height: 0.42rem;
            margin-left: 0.2rem;
          }
        }
      }
    }
    .userdes {
      width: 6rem;
      font-size: 0.3rem;
      color: #273c75;
      padding: 0.2rem 0.4rem 0.3rem 0.4rem;
    }
  }

  .confirm {
    position: fixed;
    width: 100vw;
    height: 100vh;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 0, 0, 0.6);
    .frame {
      width: 5rem;
      height: 4rem;
      margin-bottom: 2rem;
      background-color: white;
      border-radius: 0.5rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      span {
        letter-spacing: 0.1rem;
        padding-left: 0.1rem;
      }
      .confirmbtn {
        display: flex;
        justify-content: space-between;
        button {
          width: 2rem;
          height: 1rem;
          border: none;
          border-radius: 0.5rem;
          outline: none;
          background-color: #6851ac;
          color: white;
          font-size: 0.4rem;
          letter-spacing: 0.1rem;
          padding-left: 0.1rem;
          margin-top: 0.5rem;
          box-shadow: 0rem 0rem 0.2rem 0.1rem rgba(0, 0, 0, 0.1);
          &:first-child {
            margin-right: 0.23rem;
          }
        }
      }
    }
  }
}
</style>